package com.zjw.touchscreen.bluetooth;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothClass;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import androidx.core.content.ContextCompat;
import com.zjw.touchscreen.R;
import com.zjw.touchscreen.adapter.BluetoothSpinnerAdapter;
import org.apache.commons.lang3.StringUtils;

import java.util.HashSet;
import java.util.Set;

public class BluetoothBroadcastReceiver extends BroadcastReceiver {

    private Set<String> addrs = new HashSet<>();
    private BluetoothSpinnerAdapter bluetoothSpinnerAdapter;

    public BluetoothBroadcastReceiver(BluetoothSpinnerAdapter bluetoothSpinnerAdapter) {
        this.bluetoothSpinnerAdapter = bluetoothSpinnerAdapter;
    }

    @SuppressLint("MissingPermission")
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothDevice.ACTION_FOUND.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String name = device.getName();
            String address = device.getAddress();
            if (StringUtils.isBlank(name)) {
                return;
            }
            if (!addrs.add(address)) {
                return;
            }
            BluetoothListItem bluetoothListItem = new BluetoothListItem();
            switch (device.getBondState()) {
                case BluetoothDevice.BOND_BONDING:
                    bluetoothListItem.setStatus("正在配对");
                    break;
                case BluetoothDevice.BOND_BONDED:
                    bluetoothListItem.setStatus("配对成功");
                    break;
                case BluetoothDevice.BOND_NONE:
                    bluetoothListItem.setStatus("未配对");
                    break;
                default:
                    bluetoothListItem.setStatus("未知状态");
                    break;
            }
            Drawable icon=null;
            bluetoothListItem.setName(device.getName());
            BluetoothClass bluetoothClass = device.getBluetoothClass();
            int majorDeviceClass = bluetoothClass.getMajorDeviceClass();
            switch (majorDeviceClass) {
                //电脑设备
                case 0x0100:
                    icon = ContextCompat.getDrawable(context, R.drawable.computer);
                    break;
                //手机设备
                case 0x0200:
                    icon = ContextCompat.getDrawable(context, R.drawable.phone);
                    break;
                //音频视频设备，蓝牙耳机音响
                case 0x0400:
                    icon = ContextCompat.getDrawable(context, R.drawable.headset);
                    break;
                //外部设备，比如蓝牙鼠标键盘，蓝牙遥控器
                case 0x0500:
                    icon = ContextCompat.getDrawable(context, R.drawable.keyboard);
                //可穿戴手表
                case 0x0700:
                    icon = ContextCompat.getDrawable(context, R.drawable.watch);
                    break;
                default:
                    icon = ContextCompat.getDrawable(context, R.drawable.bluetooth);
            }
            bluetoothListItem.setAddr(device.getAddress());
            bluetoothListItem.setIcon(icon);
            bluetoothListItem.setBluetoothDevice(device);
            bluetoothSpinnerAdapter.refresh(bluetoothListItem);
        } else if (BluetoothDevice.ACTION_BOND_STATE_CHANGED.equals(action)) {
            BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
            String name = device.getName();
            String address = device.getAddress();
            if ((address != null || (name != null))) {
                if (device.getBondState() == BluetoothDevice.BOND_BONDED) {
                }
            }
        }
    }
}
